package android.support.v4.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;

import com.huika.dealontop.R;
import com.huika.dealontop.utils.ImageLoaderHelper;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

import java.util.ArrayList;
import java.util.List;

/**
 * @description：图片滚动类
 * @T
 * @date 2014年7月14日 下午5:44:17
 * @date 2014年7月15日 下午19, fanxing修改
 */
public class AutoScrollViewPager<T extends PagerData> extends ViewPager {
    public interface OnPageItemClickListener<T extends PagerData> {
        void onPageItemClickListener(T pd);
        void onPageItemClickListener(List<T> pagerData, int position);
    }

    private int mScrollTime = 0;
    private int oldIndex = 0;
    private int curIndex = 0;

    private List<T> mPagerData = new ArrayList<T>();
    private PagerAdapter pagerAdapter;
    private LinearLayout indicatorView;
    private int focusedDrawable, normalDrawable;
    private LayoutInflater inflater;
    private OnPageItemClickListener<T> pageItemClickListener;
    private boolean isFakeCycle = false;//是否是假的循环
    private boolean isStartScroll;
    private PageChangeCallBack mCallBack;
    private ScaleType mScaleType;

    //两个或两个以上才才显示装点
    private boolean indicatorTwoOrMore;


    public interface PageChangeCallBack {
        void onPageChange(int position);
    }

    public void setScaleType(ScaleType mScaleType) {
        this.mScaleType = mScaleType;
    }

    public AutoScrollViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);

        inflater = LayoutInflater.from(context);

        focusedDrawable = R.mipmap.current_dot;
        normalDrawable = R.mipmap.nocurrent_dot;

        setInternalPageChangeListener(new OnPageChangeListener() {
            public void onPageSelected(int i) {
                if (mCallBack != null)
                    mCallBack.onPageChange(i);
                if (mPagerData == null || mPagerData.isEmpty() || indicatorView == null
                        || indicatorView.getChildCount() < 1
                        || (indicatorTwoOrMore && indicatorView.getChildCount() < 2))
                    return;

                curIndex = i % mPagerData.size();
                // 取消圆点选中
                indicatorView.getChildAt(oldIndex).setBackgroundResource(normalDrawable);
                // 圆点选中
                indicatorView.getChildAt(curIndex).setBackgroundResource(focusedDrawable);
                oldIndex = curIndex;
            }

            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            public void onPageScrollStateChanged(int arg0) {
            }
        });

        // 设置滑动动画时间 ,如果用默认动画时间可不用 ,反射技术实现
        new FixedSpeedScroller(getContext()).setDuration(this, 700);

        pagerAdapter = new MyPagerAdapter();
        setAdapter(pagerAdapter);

        setOffscreenPageLimit(2);

        requestDisallowInterceptTouchEvent(true);

        options = new DisplayImageOptions.Builder().showImageOnLoading(R.mipmap.morenpic) // resource or drawable
                .showImageForEmptyUri(R.mipmap.morenpic) // resource or drawable
                .showImageOnFail(R.mipmap.morenpic) // resource or drawable
                .cacheOnDisk(true) // 开启硬盘缓存
                .cacheInMemory(false)   //设置图片不缓存于内存中
                .bitmapConfig(Bitmap.Config.RGB_565)    //设置图片的质量
                .imageScaleType(ImageScaleType.IN_SAMPLE_INT)    //设置图片的缩放类型，该方法可以有效减少内存的占用
                .resetViewBeforeLoading(false)
                .build();

    }

    public void setPageChangeCallback(PageChangeCallBack clallBack) {
        mCallBack = clallBack;
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        if (isStartScroll) {
            start(mScrollTime);
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (isStartScroll)
            stop();
    }

    /**
     * 构建内部索引父view
     */
    private void initInnerIndicator() {
        indicatorView = new IndicatorView(getContext());
        indicatorView.setGravity(Gravity.CENTER);
        indicatorView.setBackgroundColor(0x0fFF00FF);
        indicatorView.setOrientation(LinearLayout.HORIZONTAL);
        LayoutParams params = new LayoutParams();
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        params.height = 40;
        params.gravity = Gravity.BOTTOM;
        addView(indicatorView, params);
    }

    public void setDatas(List<T> imgUrls, LinearLayout outIndicator, boolean needInnerIndicator, boolean isFakeCycle) {
        this.isFakeCycle = isFakeCycle;
        setDatas(imgUrls, outIndicator, needInnerIndicator);
    }

    /**
     * 重复调用可能会anr
     */
    public void setDatas(List<T> imgUrls, LinearLayout outIndicator, boolean needInnerIndicator) {
        if (outIndicator != null) {
            indicatorView = outIndicator;
        } else if (needInnerIndicator) {
            initInnerIndicator();
        }
        mPagerData.clear();
        mPagerData.addAll(imgUrls);
        pagerAdapter.notifyDataSetChanged();
    }

    /**
     * 重新加载图片数据，刷新vp内容
     */
    public void refreshDatas(List<T> imgUrls, LinearLayout outIndicator, boolean needInnerIndicator, boolean isFakeCycle) {
        this.isFakeCycle = isFakeCycle;
        if (outIndicator != null) {
            indicatorView = outIndicator;
        } else if (needInnerIndicator) {
            initInnerIndicator();
        }
        mPagerData.clear();
        mPagerData.addAll(imgUrls);
        pagerAdapter = new MyPagerAdapter();
        setAdapter(pagerAdapter);
        pagerAdapter.notifyDataSetChanged();
    }

    @Override
    void dataSetChanged() {
        super.dataSetChanged();
        setUpIndicator();
    }

    // 设置圆点
    private void setUpIndicator() {
        if (indicatorView != null) {
            indicatorView.removeAllViews();
            if (mPagerData == null || mPagerData.isEmpty() || (indicatorTwoOrMore && mPagerData.size() < 2)) {
                indicatorView.setVisibility(View.GONE);
                return;
            } else {
                indicatorView.setVisibility(View.VISIBLE);
            }
            for (int i = 0; i < mPagerData.size(); i++) {
                View v = inflater.inflate(R.layout.auto_scrollpage_dot, indicatorView, false);
                v.setBackgroundResource(normalDrawable);
                indicatorView.addView(v);
            }
            indicatorView.getChildAt(0).setBackgroundResource(focusedDrawable);
        }
        curIndex = oldIndex = 0;
        if (mPagerData.size() > 1 && isFakeCycle) {
            setCurrentItem(15 - 15 % mPagerData.size());// 设置选中为中间/图片为和第0张一样
        } else {
            setCurrentItem(0);
        }
    }

    /**
     * 设置是否两页以上才显示 装点
     * @param indicatorTwoOrMore
     */
    public void setIndicatorTwoOrMore(boolean indicatorTwoOrMore) {
        this.indicatorTwoOrMore = indicatorTwoOrMore;
    }

    /**
     * 获取真实的当前位置
     */
    public int getCurrentPosition() {
        return curIndex;
    }

    /**
     * 获取真实的当前位置的数据
     */
    public T getCurrentData() throws IndexOutOfBoundsException {
        return mPagerData.get(curIndex);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_UP || ev.getAction() == MotionEvent.ACTION_CANCEL) {
            start();
        } else {
            stop();
        }
        return super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_UP || ev.getAction() == MotionEvent.ACTION_CANCEL) {
            start();
        } else {
            stop();
        }

        return super.onTouchEvent(ev);
    }

    /**
     * 开始广告滚动
     */
    private void start() {
        start(mScrollTime);
    }

    public void start(int scrollTime) {
//		handler.removeCallbacksAndMessages(null);
        mScrollTime = scrollTime;
        isStartScroll = false;
        if (mScrollTime > 0 && !mPagerData.isEmpty()) {
            isStartScroll = true;
            handler.sendMessageDelayed(handler.obtainMessage(), mScrollTime);
        }
    }

    /**
     * 通知播放
     */
    private void stop() {
        handler.removeCallbacksAndMessages(null);
    }

    private Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            stop();
            if (isFakeCycle) {
                setCurrentItem(getCurrentItem() + 1);
            } else {
                if (getCurrentItem() == mPagerData.size() - 1) {
                    setCurrentItem(0, true);
                } else {
                    setCurrentItem(getCurrentItem() + 1);
                }
            }
            handler.sendMessageDelayed(handler.obtainMessage(), mScrollTime);
        }

        ;
    };

    //	private int errorImageResId=R.drawable.product_list_default;
//	private int defaultImageResId=R.drawable.product_list_default;
    private DisplayImageOptions options;

    //	public void setErrorImageResId(int errorImageResId){
//		this.errorImageResId=errorImageResId;
//	}
//	public void setDefaultImageResId(int defaultImageResId){
//		this.defaultImageResId=defaultImageResId;
//	}
    public void setImageOptions(DisplayImageOptions options) {
        this.options = options;
    }

    // 适配器 //循环设置
    private class MyPagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            if (mPagerData.size() == 1) {
                return 1;
            } else if (mPagerData.size() > 1) {
                return isFakeCycle ? Integer.MAX_VALUE : mPagerData.size();
            }
            return 0;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            final int realP = position % mPagerData.size();
//			NetworkImageView netImgView = (NetworkImageView) inflater.inflate(R.layout.auto_scrollpage_img, container, false);
            final ImageView netImgView = (ImageView) inflater.inflate(R.layout.auto_scrollpage_img, container, false);
            if (mScaleType != null && !TextUtils.isEmpty(mPagerData.get(realP).getImageUrl())) {
                netImgView.setScaleType(mScaleType);
            }
//			netImgView.setErrorImageResId(errorImageResId);
//			netImgView.setDefaultImageResId(errorImageResId);
            //volley的imageloader缓存有问题，换成惠信的
//			ImageLoader imageLoader = VolleyRequestManager.getImageLoader();
//			netImgView.setImageUrl(mPagerData.get(realP).getImageUrl(), imageLoader);

//			GlobalApp.loadImg(netImgView,mPagerData.get(realP).getImageUrl(), options);

//            GlobalApp.loadImg(netImgView, mPagerData.get(realP).getImageUrl());

            ImageLoaderHelper.getImageLoader().displayImage(mPagerData.get(realP).getImageUrl(), netImgView, options);

            netImgView.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (pageItemClickListener != null) {
                        pageItemClickListener.onPageItemClickListener(mPagerData.get(realP));
                        pageItemClickListener.onPageItemClickListener(mPagerData,realP);
                    }
                }
            });

            container.addView(netImgView);
            return netImgView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            if (object instanceof View) {
                container.removeView((View) object);
            }
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }
    }

    /**
     * 设置显示条目的点击事件
     */
    public void setOnPageItemClickListener(OnPageItemClickListener<T> pageItemClickListener) {
        this.pageItemClickListener = pageItemClickListener;
    }
}
